Connect Device

Connect Device

Before operating the device to implement the functions of image acquisition, parameter configuration, and so on, you should connect the device (open device).

Figure 1. Programming Flow of Connecting Device

  1. Optional: Call MV_CC_EnumDevices to enumerate all devices corresponding to specified transport protocol within subnet.

    The information of found devices is returned in the structure MV_CC_DEVICE_INFO_LIST by pstDevList.

  2. Optional: Call MV_CC_IsDeviceAccessible to check if the specified device is accessible before opening it.
  3. Call MV_CC_CreateHandle to create a device handle.
  4. Call MV_CC_OpenDevice to open the device.
  5. Optional: Perform one or more of the following operations.
    Option Description

    Get Device Information

    Call MV_CC_GetDeviceInfo

    Get Optimal Package Size

    Call MV_CC_GetOptimalPacketSize

  6. Optional: Other operations, such as image acquisition and display, parameters configuration, and so on. Refer to Image Acquisition and Display for details.
  7. Call MV_CC_CloseDevice to close the device.
  8. Call MV_CC_DestroyHandle to destroy the handle and release resources.

Sample Code of Connecting Device

#include "MvCameraControl.h"
void main()
{
    int nRet = -1;
    void*  m_handle = NULL;

    //Enumerate all the corresponding devices of the specified transport protocols within the subnet.
    unsigned int nTLayerType = MV_GIGE_DEVICE | MV_USB_DEVICE;
    MV_CC_DEVICE_INFO_LIST m_stDevList = {0};
    int nRet = MV_CC_EnumDevices(nTLayerType, &m_stDevList);
    if (MV_OK != nRet)
    {
        printf("error: EnumDevices fail [%x]\n", nRet);
        return;
    }

    int i = 0;
    if (m_stDevList.nDeviceNum == 0)
    {
        printf("no camera found!\n");
        return;
    }

    //Select the searched first online device, create a device handle
    int nDeviceIndex = 0;

    MV_CC_DEVICE_INFO m_stDevInfo = {0};
    memcpy(&m_stDevInfo, m_stDevList.pDeviceInfo[nDeviceIndex], sizeof(MV_CC_DEVICE_INFO));

    nRet = MV_CC_CreateHandle(&m_handle, &m_stDevInfo);

    if (MV_OK != nRet)
    {
        printf("error: CreateHandle fail [%x]\n", nRet);
        return;
    }

    //Connect the device
    unsigned int nAccessMode = MV_ACCESS_Exclusive;
    unsigned short nSwitchoverKey = 0;
    nRet = MV_CC_OpenDevice(m_handle, nAccessMode, nSwitchoverKey);
    if (MV_OK != nRet)
    {
        printf("error: OpenDevice fail [%x]\n", nRet);
        return;
    }

    //...Other processing method

    //Close the device, release the resource
    nRet = MV_CC_CloseDevice(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: CloseDevice fail [%x]\n", nRet);
        return;
    } 
     
    //Destroy the handle, release the resource
    nRet = MV_CC_DestroyHandle(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: DestroyHandle fail [%x]\n", nRet);
        return;
    }
}

Connect Device

Before operating the device to implement the functions of image acquisition, parameter configuration, and so on, you should connect the device (open device).

Figure 1. Programming Flow of Connecting Device

  1. Optional: Call MV_CC_EnumDevices to enumerate all devices corresponding to specified transport protocol within subnet.

    The information of found devices is returned in the structure MV_CC_DEVICE_INFO_LIST by pstDevList.

  2. Optional: Call MV_CC_IsDeviceAccessible to check if the specified device is accessible before opening it.
  3. Call MV_CC_CreateHandle to create a device handle.
  4. Call MV_CC_OpenDevice to open the device.
  5. Optional: Perform one or more of the following operations.
    Option Description

    Get Device Information

    Call MV_CC_GetDeviceInfo

    Get Optimal Package Size

    Call MV_CC_GetOptimalPacketSize

  6. Optional: Other operations, such as image acquisition and display, parameters configuration, and so on. Refer to Image Acquisition and Display for details.
  7. Call MV_CC_CloseDevice to close the device.
  8. Call MV_CC_DestroyHandle to destroy the handle and release resources.

Sample Code of Connecting Device

#include "MvCameraControl.h"
void main()
{
    int nRet = -1;
    void*  m_handle = NULL;

    //Enumerate all the corresponding devices of the specified transport protocols within the subnet.
    unsigned int nTLayerType = MV_GIGE_DEVICE | MV_USB_DEVICE;
    MV_CC_DEVICE_INFO_LIST m_stDevList = {0};
    int nRet = MV_CC_EnumDevices(nTLayerType, &m_stDevList);
    if (MV_OK != nRet)
    {
        printf("error: EnumDevices fail [%x]\n", nRet);
        return;
    }

    int i = 0;
    if (m_stDevList.nDeviceNum == 0)
    {
        printf("no camera found!\n");
        return;
    }

    //Select the searched first online device, create a device handle
    int nDeviceIndex = 0;

    MV_CC_DEVICE_INFO m_stDevInfo = {0};
    memcpy(&m_stDevInfo, m_stDevList.pDeviceInfo[nDeviceIndex], sizeof(MV_CC_DEVICE_INFO));

    nRet = MV_CC_CreateHandle(&m_handle, &m_stDevInfo);

    if (MV_OK != nRet)
    {
        printf("error: CreateHandle fail [%x]\n", nRet);
        return;
    }

    //Connect the device
    unsigned int nAccessMode = MV_ACCESS_Exclusive;
    unsigned short nSwitchoverKey = 0;
    nRet = MV_CC_OpenDevice(m_handle, nAccessMode, nSwitchoverKey);
    if (MV_OK != nRet)
    {
        printf("error: OpenDevice fail [%x]\n", nRet);
        return;
    }

    //...Other processing method

    //Close the device, release the resource
    nRet = MV_CC_CloseDevice(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: CloseDevice fail [%x]\n", nRet);
        return;
    } 
     
    //Destroy the handle, release the resource
    nRet = MV_CC_DestroyHandle(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: DestroyHandle fail [%x]\n", nRet);
        return;
    }
}